<?php

class Gcentrale_GusersController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        
    	 
    	
    	
    }

    private function geteltgov($idpere = '')
    {
    	 
    	$eltgov=new Application_Model_DbTable_Governeras();
    	 
    	 
    	if (!empty($idpere)) {
    
    		$infopere=explode("|", $idpere);
    		$idretourneauperes="";
    		if($infopere[0]!="RP"){
    			$infopere =$eltgov->fetchRow("idgoverneras = '".$idpere."'")->toArray();
    		}else{
    			if(empty($infopere[2])){
    				$infopere=$eltgov->fetchRow("idperegov IS NULL AND idtypegovernera='".$infopere[1]."'")->toArray();
    			}else{
    				$infopere=$eltgov->fetchRow("idgoverneras = '".$infopere[2]."'")->toArray();
    			}
    		}
    
    		if(count($infopere)>0){
    			$idretourneauperes='RP|'.$infopere['idtypegovernera'].'|'.$infopere['idperegov'];
    		}
    		 
    		 
    		$eltlist=$eltgov->selectForOptions($idpere);
    		 
    		if(count($eltlist['data'])<=0){
    			return "";
    		}
    		 
    		if($eltlist['ispere']){
    			return (Myhelper_Utils_Library::toOptions($eltlist['data'],array('key'=>"$idretourneauperes",'value'=>'..')));
    		}else{
    			return (Myhelper_Utils_Library::toOptions($eltlist['data']));
    		}
    		 
    		 
    
    	}else{
    		$eltlist=$eltgov->selectForOptions();
    		return (Myhelper_Utils_Library::toOptions($eltlist['data'],array('key'=>'returnpere','value'=>'..')));
    	}
    }

    public function geteltgovAction()
    {
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	 
    	$dataposted=$this->getRequest()->getPost();
    	if(isset($dataposted['id'])){
    		echo $this->geteltgov($dataposted['id']);
    	}else{
    		echo $this->geteltgov();
    	}
    	 
    	 
    }

    public function gestmesrespAction()
    {
        // action body
        
    	$header=array(
    			"monidusers"=>"idusers"
    			,"monnom"	=>"nom"
    			,"monprenom"	=>"prenom"
    			,"nompere"	=>"Nom Resp."
    			,"prenompere"	=>"Prenom Resp."
    			,"monidtypeusers"=>"idtypeusers"
    			,"name"	=>"gouvernera"
    			,"monidfonctionresp"=>"idfonctionresp"
    			,"monlogin"	=>"login"
    			,"monmotpasse"	=>"motpasse"
    			,"mondelegation"	=>"delegation"
    			,"monemail"	=>"email"
    			,"montel"=>"tel"
    	);
    	$mapper = new Application_Model_DbTable_Users();
    	$this->view->headercolums=$header;
    	$identity=Zend_Auth::getInstance()->getIdentity();
    	$select=$mapper->select_user($identity->idusers);
    	//ajouter le requtte a la paginateur
    	$paginator = Zend_Paginator::factory($select);
    	//ajouter le nombre d'enregistrement par page
    	$paginator->setItemCountPerPage(3);
    	//ajouter la page par defaut
    	$paginator->setCurrentPageNumber($this->_getParam('page', 1));
    	//ajouter le paginateur au view
    	$this->view->paginator = $paginator;
    	
    	Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
    }

    public function addusersAction()
    {
        // action body
    	$form = new Application_Form_Users();
    	$this->view->form = $form;
    	 
    	if ($this->_request->isPost()) {
    		$formData = $this->_request->getPost();
    		if ($form->isValid($formData)) {
    			$user = new Application_Model_DbTable_Users();
    			$row = $user->createRow();

    			$identity=Zend_Auth::getInstance()->getIdentity();
    			$p=$form->getValue('pere');
    			if(!empty($p)){
    				$row->idpere= $p;
    			}else{
    				$row->idpere=$identity->idusers;
    			}
    			
    			
    			$row->idtypeusers = $form->getValue('type');
    			$val=$form->getValue('Regional');
    			$row->idgoverneras = $val[0];
    			$row->idfonctionresp = $form->getValue('Fonction');
    			$row->login = $form->getValue('Login');
    			$row->motpasse = $form->getValue('motdepasse');
    			$row->nom = $form->getValue('Nom');
    			
    			$row->prenom = $form->getValue('Prenom');
    		$row->delegation = $form->getValue('Delegation');
    			
    			
    			
    			$row->tel = $form->getValue('tel');
    			$row->email = $form->getValue('email');
    		//	$row->delegation=1;
    		 	$result = $row->save();
    			//On gere le resultat et l'action qui s'en suit.
    			///		print_r($result);
    			//	$form->reset();
    			 
    			 $this->_helper->redirector("gestmesresp");
    		}
    	}
    }
    
    


}





